package com.inspur.myapplication.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.inspur.myapplication.R;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeatherActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        textView=(TextView)findViewById(R.id.weather_tv);

    }

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new HttpGetClass()).start();//启动一个新线程
    }

    //将一些耗时操作放在新的线程中，而不是放在界面的主线程中。
    class HttpGetClass implements Runnable{

        @Override
        public void run() {
            try{
                URL url=new URL("http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=YOUR_KEY");
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                //设置连接的属性
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setConnectTimeout(100000);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                //建立连接
                httpURLConnection.connect();

                //判断服务器返回的响应码是否正常
                if(httpURLConnection.getResponseCode()!=HttpURLConnection.HTTP_BAD_REQUEST){
                    //通过输入流获取信息
                   BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                    String temp="";
                    StringBuffer stringBuffer=new StringBuffer();
                    while ((temp=bufferedReader.readLine())!=null){
                        stringBuffer.append(temp);
                    }
                    bufferedReader.close();
                    String content=stringBuffer.toString();
                    Log.i(WeatherActivity.class.toString(),content);

                    //解析JSON数据
                    JSONObject jsonObject=new JSONObject(content);
                    String resultcode=jsonObject.getString("resultcode");

                    if(resultcode.equals("200")){
                        jsonObject=jsonObject.getJSONObject("result");
                        jsonObject=jsonObject.getJSONObject("sk");
                        final String wendu=jsonObject.getString("temp");
                        Log.i(WeatherActivity.class.toString(),wendu);

                        //不可在其他线程中更新主线程中的界面,比如textView.setText();
                        textView.post(new Runnable() {
                            @Override
                            public void run() {
                                textView.setText(wendu);
                            }
                        });//将参加中执行的代码，在主线程中运行。

                    }
                }



                httpURLConnection.disconnect();
            }catch (Exception e){
                Log.e(HttpGetClass.class.toString(),e.toString());
            }
        }
    }
}
